﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PES_TurnierManager.Controller
{
    public class PESTournametController : ITournamentController
    {
        Model.Model mModel;

        public PESTournametController()
        {
        }

        public void setModel(Model.Model model)
        {
            mModel = model;
        }

        /// <summary>
        /// Adds a Group to the model.
        /// </summary>
        /// <param name="name">name of the Group</param>
        /// <returns>false if a group with the passed name already exists</returns>
        public bool addGroup(string name, bool with2Legs)
        {
            if (mModel.existGroup(name) == false)
            {
                //Group with passed name doesnt exist --> create it
                mModel.addGroup(name, with2Legs);
                return true;
            }

            MessageBox.Show("Es existiert schon eine Gruppe mit dem Namen " + name);
            return false;
        }

        /// <summary>
        /// Adds a player to a group
        /// </summary>
        /// <param name="groupName">groupname</param>
        /// <param name="playerName">name of the player</param>
        /// <param name="teamName">name of the team the player plays with</param>
        public bool addPlayerToGroup(string groupName, string playerName, string teamName)
        {
            if (!mModel.existGroup(groupName))
            {
                MessageBox.Show("Gruppe Existiert nicht!");
                return false;
            }

            if (mModel.getGroup(groupName).existPlayer(playerName))
            {
                MessageBox.Show("Gruppe " + groupName + "enthält schon einen Spieler " + playerName);
                return false;
            }

            mModel.addPlayerToGroup(groupName, playerName, teamName);
            return true;
        }

        public void generateMatchesForGroup(string groupName)
        {
            if (!mModel.existGroup(groupName))
            {
                MessageBox.Show("Gruppe " + groupName + " existiert nicht!", "Fehler");
                return;
            }

            mModel.generateMatchesForGroup(groupName);
        }

        public void resultForMatch(string groupName, uint matchID, uint homeScore, uint guestScore)
        {
            if (!mModel.existGroup(groupName))
            {
                MessageBox.Show("Gruppe " + groupName + " existiert nicht!", "Fehler");
                return;
            }    
            mModel.addResultForMatch(groupName, matchID, homeScore, guestScore);
        }

        /// <summary>
        /// Removes a player from the group
        /// </summary>
        /// <param name="playerName"></param>
        /// <param name="groupName"></param>
        public void removePlayer(string playerName, string groupName)
        {
            if (mModel.hasGroupStarted(groupName))
            {
                //We can not remove the player since the group has already 
                //been started
                MessageBox.Show("Nicht möglich. Gruppe schon gestartet");
            }
            else if (!mModel.getGroup(groupName).existPlayer(playerName))
            {
                //Thers is no group with the passed name
                MessageBox.Show("Spieler " + playerName + "existiert nicht in Gruppe " + groupName);
            }
            else
            {
                //remove the player
                mModel.removePlayerFromGroup(playerName, groupName);
            }
        }


    }
}
